package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.BaseActivity;
import com.lottery.ruok.lottrey.activity.LoginActivity;
import com.lottery.ruok.lottrey.retrofitModule.response.YiLouResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.IToast;
import utils.StringUtils;
import utils.Utils;

/**
 * 作者：lm
 * <p>
 * 创建日期：2018/8/21$
 * <p>
 * 描述：遗漏适配器
 */
public class YiLouAdapter extends RecyclerView.Adapter<YiLouAdapter.YiLouHolder> {
    private Context context;
    private List<YiLouResponse> dataBeans;
    private yiLouClickListener yiLouClickListener;

    public YiLouAdapter(Context context, List<YiLouResponse> data) {
        this.context = context;
        this.dataBeans = data;
    }

    public void setYiLouClickListener(yiLouClickListener clickListener) {
        this.yiLouClickListener = clickListener;
    }

    @Override
    public YiLouHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new YiLouHolder(LayoutInflater.from(context).inflate(R.layout.item_fragment_yi_lou, null));
    }

    @Override
    public void onBindViewHolder(final YiLouHolder holder, final int position) {
        if (dataBeans.get(position) != null) {
            YiLouResponse bean = dataBeans.get(position);
            holder.llLotteryBuy.setVisibility(View.GONE);
            holder.tvLotteryBuy.setTextColor(context.getResources().getColor(R.color.color5));
            holder.tvLotteryBuy.setBackground(context.getResources().getDrawable(R.mipmap.cardno));
            holder.tvLotteryMoney.setImeOptions(EditorInfo.IME_ACTION_DONE);
            holder.tvLotteryName.setText(bean.getOff_name());
            holder.tvLotteryWhich.setText("" + bean.getOff_count());
            holder.tvLotteryOdds.setText(bean.getDan_odds());
            holder.tvLotteryBuy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    yiLouClickListener.onYiLouClick(position, 1, "");
                    holder.llLotteryBuy.setVisibility(View.VISIBLE);
                    holder.tvLotteryBuy.setTextColor(context.getResources().getColor(R.color.tab_selected));
                    holder.tvLotteryBuy.setBackground(context.getResources().getDrawable(R.mipmap.card));
                }
            });
            holder.tvLotteryQuit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.llLotteryBuy.setVisibility(View.GONE);
                    holder.tvLotteryBuy.setTextColor(context.getResources().getColor(R.color.color5));
                    holder.tvLotteryBuy.setBackground(context.getResources().getDrawable(R.mipmap.cardno));
                }
            });
            holder.tvLotterySure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String money = holder.tvLotteryMoney.getText().toString().trim();

                    if (StringUtils.isEmpty(money)) {
                        IToast.show("请检查输入金额", context);
                    } else {
                        yiLouClickListener.onYiLouClick(position, 1, money);
                    }
                }

            });

        }
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public class YiLouHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_lottery_name)
        TextView tvLotteryName;
        @BindView(R.id.tv_lottery_which)
        TextView tvLotteryWhich;
        @BindView(R.id.tv_lottery_buy)
        TextView tvLotteryBuy;
        @BindView(R.id.tv_lottery_odds)
        TextView tvLotteryOdds;
        @BindView(R.id.tv_lottery_money)
        EditText tvLotteryMoney;
        @BindView(R.id.tv_lottery_quit)
        TextView tvLotteryQuit;
        @BindView(R.id.tv_lottery_sure)
        TextView tvLotterySure;
        @BindView(R.id.ll_lottery_buy)
        LinearLayout llLotteryBuy;

        public YiLouHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface yiLouClickListener {
        void onYiLouClick(int posion, int v, String moner);
    }
}
